package com.framework.modules.job.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.flow.FlowFrozenTerminal;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysAgentGrade;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.service.*;
import com.framework.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author wq
 * @date 2019/9/4 17:02
 * @description
 */
@Component("tlIndirectTask")
public class TlIndirectTask {


    @Autowired
    private FlowFrozenTerminalService flowFrozenTerminalService;

    @Autowired
    private AgentService agentService;

    @Autowired
    private  AgentGradeService agentGradeService;

    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private AgentInfoService agentInfoService;

    @Autowired
    private FlowAccountService flowAccountService;



    private final static  BigDecimal STANDARD=new BigDecimal("39000");
    private final static  BigDecimal AMOUNT=new BigDecimal("100");

    public void indirectAmount(){

        List<FlowFrozenTerminal> terminals = flowFrozenTerminalService.selectList(
                new EntityWrapper<FlowFrozenTerminal>()
                .eq("indirect_state",0));


        for (FlowFrozenTerminal terminal : terminals) {
            //自己
            SysAgent agent = agentService.queryAgentById(terminal.getAgentId());
            if(agent!=null){
                //上级
                SysAgent sysAgent = agentService.queryAgentById(agent.getPid());
                //非空
                if(sysAgent==null){
                    continue;
                }
                //上级的上级
                SysAgent targetAgent = agentService.queryAgentById(sysAgent.getPid());

                if(targetAgent==null){
                    continue;
                }

                SysAgent basicAgentByDeptId = agentService.queryBasicAgentBydeptId(terminal.getDeptId());
                //非平台
                if(basicAgentByDeptId.getAgentId().equals(targetAgent.getAgentId())){
                    continue;
                }

                //检查是否是合格的客户经理
                FlowFrozenTerminal ter= flowFrozenTerminalService.queryByAgentId(targetAgent.getAgentId());
                if(ter==null && targetAgent.getStandardState().intValue()==0 ){
                    continue;
                }

                List<TranTransactionInfo> list = transactionInfoService.selectList(new EntityWrapper<TranTransactionInfo>().eq("sn", terminal.getSn())
                        .ge("transaction_date", DateUtils.format(terminal.getCreateTime(), DateUtils.DATE_PATTERN))
                );
                BigDecimal amount = list.stream().map(TranTransactionInfo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                //交易量达标
                if(amount.compareTo(STANDARD)!=-1){

                    SysAgentGrade targetGrade = agentGradeService.queryGradeByAgentId(targetAgent.getAgentId());

                    SysAgentGrade agentGrade = agentGradeService.queryGradeByAgentId(agent.getAgentId());

                    if(targetGrade.getThisGrade().intValue()>agentGrade.getThisGrade().intValue()){
                        System.out.println("间推奖励目标"+targetAgent.getName());
                        agentInfoService.addAmount(targetAgent.getAgentId(),AMOUNT);

                        FlowAccount account=new FlowAccount();

                        account.setRemark("间推"+agent.getName()+"奖励");
                        account.setAccountType(8);
                        account.setSupportType(2);
                        account.setBelId(terminal.getCountId().toString());
                        account.setCreateTime(new Date());
                        account.setAgentId(targetAgent.getAgentId());
                        account.setAmount(AMOUNT);
                        flowAccountService.insert(account);
                        terminal.setIndirectState(1);
                        flowFrozenTerminalService.updateById(terminal);
                    }
                }
            }

        }

    }

}
